package uutalk.netcloud.talk.app.ui;


import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.Constants;
import uutalk.netcloud.talk.app.ui.fragment.GroupHistoryFragment;
import uutalk.netcloud.talk.app.ui.fragment.GroupListFragment;
import uutalk.netcloud.talk.app.ui.Activity.GroupMainActivity;
import uutalk.netcloud.talk.app.ui.fragment.SigleFragment;
import uutalk.netcloud.talk.app.ui.fragment.SigleTalkFragment;
import uutalk.netcloud.talk.app.ui.fragment.UserInfoFragment;
import uutalk.netcloud.talk.base.android.BaseActivity;
import uutalk.netcloud.talk.model.bean.JoinSingleTalkRsp;
import uutalk.netcloud.talk.model.bean.SimpleEvent;
import uutalk.netcloud.talk.model.bean.SimpleEventType;

/**
 * Created by Administrator on 2017/11/8.
 */

public class ContentActivity extends BaseActivity {

    Fragment baseFragment;

    @Override
    public void init() {
        swichFragment(getIntent());
    }

    @Override
    public int layoutId() {
        return R.layout.activity_content;
    }

    @Override
    public boolean autoBindEvent() {
        return true;
    }

    @Override
    public boolean supportActionbar() {
        return false;
    }

    public void swichFragment(Intent intent){

        int fragmentKey = intent.getIntExtra(Constants.KEY_FRAGMENT,0);
        int str = intent.getIntExtra(Constants.C0NTACTS_WHERE,0);
        String keyID = intent.getStringExtra(Constants.KEY_UID);
        switch (fragmentKey){
            case Constants.SIGLE_FRAGMENT:{
                replaceFragment(SigleFragment.newInstance());
            } break;
            case Constants.GROUP_FRAGMENT:{
                replaceFragment(GroupListFragment.newInstance());
            } break;
            case Constants.USER_INFO_FRAGMENT:{//群组个人信息
                replaceFragment(UserInfoFragment.newInstance(keyID));
            }break;
            case Constants.GROUP_HISTORY_FRAGMENT:{//群聊聊历史记录
                int groupType = intent.getIntExtra(Constants.KEY_TYPE, 0);
                replaceFragment(GroupHistoryFragment.newInstance(keyID, groupType));
            } break;
            case Constants.SIGLE_TALK_FRAGMENT:{
                replaceFragment(SigleTalkFragment.newInstance(keyID,
                        intent.getIntExtra(Constants.KEY_TYPE, 0),
                        (JoinSingleTalkRsp)intent.getParcelableExtra(Constants.KEY_OBJ))
                );
//                replaceFragment(SigleMainFragment.newInstance(keyID,
//                        intent.getIntExtra(Constants.KEY_TYPE, 0),
//                        (JoinSingleTalkRsp)intent.getParcelableExtra(Constants.KEY_OBJ))
//                );
            } break;
//            case Constants.GROUP_INFO_FRAGMENT:{
//                replaceFragment(GroupMainActivity.newInstance(keyID, intent.getStringExtra(Constants.KEY_TITLE)));
//            } break;

            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSimpleEventMessage(SimpleEvent event) {
        if(event.type == SimpleEventType.MSG_FINISH_FRAGMEMT){
            finish();
        }
    }


    public void replaceFragment(Fragment fragmnet){
        replaceFragment(R.id.fragmentContent,fragmnet);
        baseFragment = fragmnet;
    }

    public void replaceFragment(@IdRes int id, Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(id, fragment).commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(baseFragment != null && baseFragment instanceof SigleTalkFragment){
            return ((SigleTalkFragment)baseFragment).onKeyDown(keyCode, event);
        }

        return super.onKeyDown(keyCode, event);
    }
}
